﻿using System;
using System.Web;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Mozlite.Data;
using Mozlite.Components;

namespace Mozlite.WebUI.Mvc4
{
    /// <summary>
    /// 前台页面基类。
    /// </summary>
    /// <typeparam name="TModel">模型类型。</typeparam>
    public abstract class WebViewPage<TModel> : System.Web.Mvc.WebViewPage<TModel>
    {
        #region views
        private string action;
        /// <summary>
        /// 获取当前请求的Action名称。
        /// </summary>
        public string Action {
            get {
                if (action == null)
                    action = ViewContext.RouteData.Get<string>("action");
                return action;
            }
        }

        private string controller;
        /// <summary>
        /// 获取当前请求的Controller名称。
        /// </summary>
        public string Controller
        {
            get
            {
                if (controller == null)
                    controller = ViewContext.RouteData.Get<string>("controller");
                return controller;
            }
        }
        #endregion

        #region pager
        private int pageIndex = -1;
        /// <summary>
        /// 页码，从1开始。
        /// </summary>
        public int PageIndex
        {
            get
            {
                if (pageIndex == -1)
                {
                    pageIndex = ViewContext.RouteData.Get<int>("Page", Current.PageIndex);
                    if (pageIndex < 1)
                        pageIndex = 1;
                }
                return pageIndex;
            }
        }

        private int pageSize = 0;
        /// <summary>
        /// 每页显示的记录数。
        /// </summary>
        public int PageSize
        {
            get
            {
                if (pageSize == 0)
                {
                    pageSize = ViewContext.RouteData.Get<int>("Page", Current.PageSize);
                }
                return pageSize;
            }
            set
            {
                pageSize = value;
                Current.PageSize = value;
            }
        }
        #endregion

        #region resources
        /// <summary>
        /// 资源名称区域语言。
        /// </summary>
        protected virtual string CultureName
        {
            get
            {
                return Current.CultureName;
            }
        }
        /// <summary>
        /// 获取当前语言资源字符串。
        /// </summary>
        /// <param name="key">资源键对象。</param>
        /// <returns>返回资源字符串。</returns>
        public virtual string L(object key)
        {
            return Cores.GetGlobalResourceByKey(key);
        }

        /// <summary>
        /// 获取当前语言资源字符串。
        /// </summary>
        /// <param name="key">资源键对象。</param>
        /// <param name="args">格式化参数。</param>
        /// <returns>返回格式化后的资源字符串。</returns>
        public virtual string L(object key, params object[] args)
        {
            return Cores.GetGlobalResourceByKey(key).FormatWith(args);
        }

        /// <summary>
        /// 获取当前语言资源字符串。
        /// </summary>
        /// <param name="key">资源键对象。</param>
        /// <returns>返回资源字符串。</returns>
        public IHtmlString LRaw(object key)
        {
            return Html.Raw(L(key));
        }

        /// <summary>
        /// 获取当前语言资源字符串。
        /// </summary>
        /// <param name="key">资源键对象。</param>
        /// <param name="args">格式化参数。</param>
        /// <returns>返回格式化后的资源字符串。</returns>
        public IHtmlString LRaw(object key, params object[] args)
        {
            return Html.Raw(L(key, args));
        }

        private void ShowMessage(string key, string message, params object[] args)
        {
            ViewBag.Status = key;
            ViewBag.Message = message.FormatWith(args);
        }
        /// <summary>
        /// 显示信息。
        /// </summary>
        /// <param name="message">信息字符串。</param>
        /// <param name="args">参数。</param>
        protected virtual void ShowInformation(string message, params object[] args)
        {
            ShowMessage("info", message, args);
        }
        /// <summary>
        /// 显示成功信息。
        /// </summary>
        /// <param name="message">信息字符串。</param>
        /// <param name="args">参数。</param>
        protected virtual void ShowSuccess(string message, params object[] args)
        {
            ShowMessage("success", message, args);
        }
        /// <summary>
        /// 显示错误信息。
        /// </summary>
        /// <param name="message">信息字符串。</param>
        /// <param name="args">参数。</param>
        protected virtual void ShowError(string message, params object[] args)
        {
            ShowMessage("error", message, args);
        }
        /// <summary>
        /// 显示资源信息。
        /// </summary>
        /// <param name="message">信息字符串。</param>
        /// <param name="args">参数。</param>
        protected virtual void ShowLocalInformation(string key, params object[] args)
        {
            ShowMessage("info", L(key, args));
        }
        /// <summary>
        /// 显示成功资源信息。
        /// </summary>
        /// <param name="message">信息字符串。</param>
        /// <param name="args">参数。</param>
        protected virtual void ShowLocalSuccess(string key, params object[] args)
        {
            ShowMessage("success", L(key, args));
        }
        /// <summary>
        /// 显示错误资源信息。
        /// </summary>
        /// <param name="message">信息字符串。</param>
        /// <param name="args">参数。</param>
        protected virtual void ShowLocalError(string key, params object[] args)
        {
            ShowMessage("error", L(key, args));
        }
        #endregion

        #region common
        private User user;
        /// <summary>
        /// 当前登录实例对象。
        /// </summary>
        public User Self {
            get {
                if (user == null)
                    user = Current.User;
                return user;
            }
        }

        private AppContext current;
        /// <summary>
        /// 获取当前应用程序上下文实例。
        /// </summary>
        public AppContext Current
        {
            get
            {
                if(current == null)
                    current = AppContext.Current;
                return current;
            }
        }

        private GlobalSettings gSettings;
        /// <summary>
        /// 获取全局设置。
        /// </summary>
        public GlobalSettings GSettings
        {
            get
            {
                if(gSettings == null)
                    gSettings = GlobalSettings.Current;
                return gSettings;
            }
        }

        private SiteSettings siteSettings;
        /// <summary>
        /// 获取网站设置。
        /// </summary>
        public SiteSettings SiteSettings
        {
            get
            {
                if(siteSettings == null)
                    siteSettings = SiteSettings.Current;
                return siteSettings;
            }
        }
        #endregion
    }
}
